//服务端接口路径
var server = Config.upurl + "/api/v2/sbxj/upload";
var AppId = mui.baseApp.AppId; //APPid
var AppKey = mui.baseApp.AppKey; // AppKey
var AppSecret = mui.baseApp.AppSecret; // AppSecret
var Version = localStorage.getItem('version'); // 版本
var timestamp = Date.parse(new Date()); // 时间戳
var OSType = (mui.os.ios) ? "ios" : "android"; // 系统
var DGUID;
if (localStorage.getItem("uuid")) {
	DGUID = localStorage.getItem("uuid");
} else {
	DGUID = plus.device.uuid;
	localStorage.setItem("uuid", param.DGUID);
}

var page = {
	imgUp: function(cb) {
		plus.nativeUI.actionSheet({
			cancel: "取消",
			buttons: [{
					title: "拍照"
				},
				{
					title: "从相册中选择"
				}
			]
		}, function(e) { // 1 是拍照  2 从相册中选择 
			switch(e.index) {
				case 1:
					appendByCamera(cb)
					break;
				case 2:
					appendByGallery(cb);
					break;
			}
		});
	}
}

// 拍照添加文件
function appendByCamera(cb) {
	plus.camera.getCamera().captureImage(function(e) {
		// console.log("e is" + e);
		plus.gallery.save(e, function(event) {
			// console.log('save gallery: ' + JSON.stringify(event))
			plus.io.resolveLocalFileSystemURL(e, function(entry) {
				var path = entry.toLocalURL();
				// console.log(path)
				// 获取图片的地址
				compressImage(path, cb)
			}, function(e) {
				plus.nativeUI.toast("上传错误：" + e.message);
			})
		})
	}, function(e) {
		plus.nativeUI.toast("读取拍照文件错误：请先开启相机权限");
	});
}

// 从相册添加文件
function appendByGallery(cb) {
	plus.gallery.pick(function(path) {
		compressImage(path, cb);
	});
}

//  压缩图片
function compressImage(path, cb) {
	var itool = {
		options: {
			multiple: false,
			resize: {
				height: 0,
				width: 0
			},
			isResize: false,
			isZoom: true, // 默认通过native.js压缩
			zoomBox: 8000, // 缩放宽高，默认1200px,横图缩放宽度，高度根据比例计算，同理竖图
			zoomQuality: mui.os.ios ? 90 : 100, //压缩图片的质量
			width: "100%", // 压缩图片的宽度
		}
	}
	var name = path.substr(path.lastIndexOf('/') + 1);
	console.log(name)
	var names = name.split(".");
	var ext = names[names.length - 1];
	// 生成随机文件名
	var uuid = UUID.prototype.createUUID();
	var filename = uuid + "." + ext;
	plus.zip.compressImage({
		src: path,
		dst: '_doc/' + filename,
		overwrite: true,
		quality: itool.options.ZoomQuality,
		width: itool.options.width
	}, function(zip) {
		console.log("图片压缩后地址" + zip.target);
		if (cb) {
			cb(zip.target); // 获取压缩后的图片地址
		}
		// 压缩图片后开始上传 
		// upload(zip.target, uuid);
	}, function(zipe) {
		// 图片压缩失败
		console.log('压缩失败')
	});
}

// 上传文件
function upload(path, cb) {
	var wt = plus.nativeUI.showWaiting();
	if (plus.networkinfo.getCurrentType() > 1)  {
		var task = plus.uploader.createUpload(server + "?AppId=" + AppId + "&AppKey=" + AppKey + "&AppSecret=" + AppSecret + "&version=" + Version + "&timestamp=" + timestamp + "&OSType=" + OSType + "&DGUID=" + DGUID, {
				method: "POST"
			},
			function(t, status) { // 上传完成
				wt.close();
				if(status == 200) {
					// console.log("上传成功：" + t.responseText);
					var res = JSON.parse(t.responseText)
					if(res.status == 200) {
						// console.log('返回路径：' + res.data.savePath)
						if (cb) {
							cb(res.data.savePath)
						}
					} else if(res.status == 400) {
						mui.toast(res.message)
					}
					wt.close(); //关闭等待提示按钮
				} else {
					console.log("上传失败：" + status);
				}
			}
		);
		//			//添加其他参数
		//	task.setRequestHeader('ContentType', 'multipart/form-data');
		task.addData("file", path);
		task.addFile(path, {
			key: path
		});
		task.start();
	} else {
		console.log('无网状态下地址' + path);
		wt.close(); //关闭等待提示按钮
	}
}